unit main;

{
  Main formular, used to select the systems
}

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  ExtCtrls, StateMachine, windows;



type
  TDWordFiller = record
  {$IFDEF CPUX64}
    Filler: array[1..4] of Byte; // Pad DWORD to make it 8 bytes (4+4) [x64 only]
  {$ENDIF}
  end;

  TWMHotKey = record
    Msg: Cardinal;
    MsgFiller: TDWordFiller;
    HotKey: WPARAM;
    Unused: LPARAM;
    Result: LRESULT;
  end;

  { TMainForm }

  TMainForm = class(TForm)
    StartBtn: TButton;
    IOSelBox: TComboBox;
    GroupBox1: TGroupBox;
    AISelBox: TComboBox;
    GUISelBox: TComboBox;
    Panel1: TPanel;
    Panel2: TPanel;
    Panel3: TPanel;
    procedure FormCreate(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure StartBtnClick(Sender: TObject);
  private
     procedure WMHotKey(var Msg: TWMHotKey); message WM_HOTKEY;
    { private declarations }
  public
    procedure SetupSelectionProcess;
    { public declarations }
  end;

var
  MainForm: TMainForm;

implementation

uses
  SystemManager;

{$R *.lfm}

{ TMainForm }

procedure TMainForm.StartBtnClick(Sender: TObject);
begin
  GlobalSystemManager.SelectedGUI := GUISelBox.ItemIndex;
  GlobalSystemManager.SelectedIO  := IOSelBox.ItemIndex;
  GlobalSystemManager.SelectedAI  := AISelBox.ItemIndex;
  Visible := false;
  GlobalStateMachine.Transit(SS_SetupSystems);
end;

procedure TMainForm.WMHotKey(var Msg: TWMHotKey);
begin
  GlobalSystemManager.Timer.Enabled := not GlobalSystemManager.Timer.Enabled;
end;

procedure TMainForm.FormShow(Sender: TObject);
begin
  GlobalStateMachine.Transit(SS_SelectSystems);
end;

procedure TMainForm.FormCreate(Sender: TObject);
begin
  RegisterHotKey(Handle, 1, 0, VK_ESCAPE );
end;

procedure TMainForm.SetupSelectionProcess;
var
  i: integer;
begin
  GlobalSystemManager.ClearSystems;
  GUISelBox.Items.clear;
  for i := 0 to GlobalSystemManager.GUI_Systems.Count-1 do
  begin
    GUISelBox.Items.Add(GlobalSystemManager.GUI_Systems.Items[i].ClassName);
  end;
  GUISelBox.ItemIndex := 0;

  IOSelBox.Items.clear;
  for i := 0 to GlobalSystemManager.IO_Systems.Count-1 do
  begin
    IOSelBox.Items.Add(GlobalSystemManager.IO_Systems.Items[i].ClassName);
  end;
  IOSelBox.ItemIndex := 0;

  AISelBox.Items.clear;
  for i := 0 to GlobalSystemManager.AI_Systems.Count-1 do
  begin
    AISelBox.Items.Add(GlobalSystemManager.AI_Systems.Items[i].ClassName);
  end;
  AISelBox.ItemIndex := 0;

  if not Visible then Visible := true;
end;

end.

